home *** CD-ROM | disk | FTP | other *** search
- // This is a part of the Sheriff System Development Kit.
- // Copyright (C) 1997-1998 Acudata Limted.
- // All rights reserved.
- //
- // This source code is only intended as a supplement to the
- // Sheriff System Development Kit and related
- // electronic documentation provided with the SDK.
-
- #include "stdafx.h"
- #include <stdlib.h>
- #include <stdio.h>
- #include <time.h>
-
- #include "Sheriff.h"
-
- CSheriff::CSheriff(LPCTSTR lpszProductID,LPCTSTR lpszUserName)
- {
- m_strProductID=lpszProductID;
- m_strUserName=lpszUserName;
-
- //Zero secrets
- for(int i=0;i<4;i++)
- memset(m_arySecrets[i].Secret,0,sizeof(SLS_SECRET));
- m_bSecretsSet=FALSE;
- //
- m_hLicence=0L;
- m_lLastError=SLS_SUCCESS;
- }
-
- CSheriff::~CSheriff()
- {
- }
-
- void CSheriff::SetSecrets(SLS_SECRET *pSecrets,int nSizeSecrets)
- {
- for(int i=0;i<min(nSizeSecrets,4);i++)
- memcpy(m_arySecrets[i].Secret,pSecrets[i].Secret,sizeof(SLS_SECRET));
- m_bSecretsSet=TRUE;
- }
-
- void CSheriff::GetLastErrorMessage(CString &strError)
- {
- char szErrorMessage[256];
- SLS_GetErrorMessage(m_lLastError,szErrorMessage);
- strError=szErrorMessage;
- }
-
- BOOL CSheriff::QueryLicenceInfo(SLS_LICENCE_INFO &licInfo)
- {
- m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::GetReference(CString &strReference)
- {
- char szReference[128];
- m_lLastError=SLS_GetReference(m_strProductID,szReference);
- strReference=szReference;
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::Register(LPCTSTR lpszProductName,LPCTSTR lpszLicencePath)
- {
- m_lLastError=SLS_Register(m_strProductID,lpszProductName,lpszLicencePath);
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::SetLicence(LPCSTR pszReference,LPCTSTR lpszLicenceKey)
- {
- m_lLastError=SLS_SetLicence(m_strProductID,pszReference,lpszLicenceKey);
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::License(SLS_LICENCE Licence)
- {
- if(!m_bSecretsSet)
- {
- //Secrets Undefined
- m_lLastError=SLS_E_BAD_SECRET;
- return FALSE;
- }
-
- //Create Challenge
- SLS_CHALLENGE Challenge;
- m_lLastError=SLS_CreateChallenge(m_arySecrets,4,(const BYTE *)&Licence,sizeof(SLS_LICENCE),&Challenge);
- if(!SUCCEEDED(m_lLastError))
- return FALSE;
- m_lLastError=SLS_License(m_strProductID,&Licence,&Challenge);
-
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::Request(SLS_REQUEST Request,SLS_PERMIT &Permit)
- {
- memset(&Permit,0,sizeof(Permit));
-
- SLS_CHALLENGE Challenge;
- if(m_bSecretsSet)
- {
- //Create Challenge
- m_lLastError=SLS_CreateChallenge(m_arySecrets,4,(const BYTE *)&Request,sizeof(SLS_REQUEST),&Challenge);
- if(!SUCCEEDED(m_lLastError))
- return FALSE;
- m_lLastError=SLS_Request(m_strProductID,m_strUserName,&Request,&Permit,&m_hLicence,&Challenge);
- if(SUCCEEDED(m_lLastError))
- {
- //Verify Challenge
- m_lLastError=SLS_VerifyChallenge(m_arySecrets,4,(const BYTE *)&Permit,sizeof(SLS_PERMIT),&Challenge);
- }
- }
- else
- {
- //No Challenge
- Challenge.Protocol=SLS_NO_PROTOCOL;
- m_lLastError=SLS_Request(m_strProductID,m_strUserName,&Request,&Permit,&m_hLicence,&Challenge);
- }
-
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::Update(SLS_UPDATE Update,SLS_PERMIT &Permit)
- {
- memset(&Permit,0,sizeof(Permit));
-
- SLS_CHALLENGE Challenge;
- if(m_bSecretsSet)
- {
- //Create Challenge
- m_lLastError=SLS_CreateChallenge(m_arySecrets,4,(const BYTE *)&Update,sizeof(SLS_UPDATE),&Challenge);
- if(!SUCCEEDED(m_lLastError))
- return FALSE;
- m_lLastError=SLS_Update(m_strProductID,m_hLicence,&Update,&Permit,&Challenge);
- if(SUCCEEDED(m_lLastError))
- {
- //Verify Challenge
- m_lLastError=SLS_VerifyChallenge(m_arySecrets,4,(const BYTE *)&Permit,sizeof(SLS_PERMIT),&Challenge);
- }
- }
- else
- {
- //No Challenge
- Challenge.Protocol=SLS_NO_PROTOCOL;
- m_lLastError=SLS_Update(m_strProductID,m_hLicence,&Update,&Permit,&Challenge);
- }
-
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::Release(SLS_RELEASE Release)
- {
- SLS_CHALLENGE Challenge;
- if(m_bSecretsSet)
- {
- //Create Challenge
- SLS_CreateChallenge(m_arySecrets,4,(const BYTE *)&Release,sizeof(SLS_RELEASE),&Challenge);
- }
- else
- {
- //No Challenge
- Challenge.Protocol=SLS_NO_PROTOCOL;
- }
-
- m_lLastError=SLS_Release(m_strProductID,m_hLicence,&Release,&Challenge);
-
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::IsProductInstalled()
- {
- m_lLastError=SLS_IsProductInstalled(m_strProductID);
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::IsProductRegistered()
- {
- m_lLastError=SLS_IsProductRegistered(m_strProductID);
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::IsProductLicensed()
- {
- m_lLastError=SLS_IsProductLicensed(m_strProductID);
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::IsLicenceDefined()
- {
- SLS_LICENCE_INFO licInfo;
-
- m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
- if(!SUCCEEDED(m_lLastError))
- return FALSE;
- if(licInfo.State==SLS_STATE_UNDEFINED || licInfo.State==SLS_STATE_BAD)
- return FALSE;
- return TRUE;
- }
-
- BOOL CSheriff::IsLicenceValid()
- {
- SLS_LICENCE_INFO licInfo;
-
- m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
- if(!SUCCEEDED(m_lLastError))
- return FALSE;
- return licInfo.State==SLS_STATE_OK;
- }
-
- BOOL CSheriff::IsLicenceExportable()
- {
- SLS_LICENCE_INFO licInfo;
-
- m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
- if(!SUCCEEDED(m_lLastError))
- return FALSE;
- return !(licInfo.Type & SLS_TYPE_UNEXPORTABLE);
- }
-
- BOOL CSheriff::IsStandaloneKey()
- {
- SLS_LICENCE_INFO licInfo;
-
- m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
- if(!SUCCEEDED(m_lLastError))
- return FALSE;
- return (licInfo.Type & SLS_TYPE_STANDALONE);
- }
-
- BOOL CSheriff::IsLifetimeKey()
- {
- SLS_LICENCE_INFO licInfo;
-
- m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
- if(!SUCCEEDED(m_lLastError))
- return FALSE;
- return (licInfo.Type & SLS_TYPE_LIFETIME_KEY);
- }
-
- BOOL CSheriff::IsLifetimeRef()
- {
- SLS_LICENCE_INFO licInfo;
-
- m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
- if(!SUCCEEDED(m_lLastError))
- return FALSE;
- return (licInfo.Type & SLS_TYPE_LIFETIME_REF);
- }
-
- BOOL CSheriff::SetOptions(SLS_OPTIONS Options)
- {
- if(!m_bSecretsSet)
- {
- //Secrets Undefined
- m_lLastError=SLS_E_BAD_SECRET;
- return FALSE;
- }
-
- //Create Challenge
- SLS_CHALLENGE Challenge;
- m_lLastError=SLS_CreateChallenge(m_arySecrets,4,(const BYTE *)&Options,sizeof(SLS_OPTIONS),&Challenge);
- if(!SUCCEEDED(m_lLastError))
- return FALSE;
- m_lLastError=SLS_SetOptions(m_strProductID,m_hLicence,&Options,&Challenge);
-
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::Export(SLS_LICENCE Licence,LPCTSTR lpszExportRef,CString &strExportKey)
- {
- char szExportKey[128];
- m_lLastError=SLS_ExportLicence(m_strProductID,lpszExportRef,&Licence,szExportKey);
- strExportKey=szExportKey;
- return SUCCEEDED(m_lLastError);
- }
-
- BOOL CSheriff::Import(LPCSTR pszReference,LPCTSTR lpszLicenceKey)
- {
- m_lLastError=SLS_ImportLicence(m_strProductID,pszReference,lpszLicenceKey);
- return SUCCEEDED(m_lLastError);
- }
-
- DWORD CSheriff::GetUserCount()
- {
- DWORD dwUserCount;
- SLS_GetUserCount(m_strProductID,&dwUserCount);
- return dwUserCount;
- }
-
- BOOL CSheriff::QueryUserInfo(DWORD dwUserIndex,SLS_USER_INFO &userInfo)
- {
- //dwUserIndex is 1 based
- DWORD dwUserCount=GetUserCount();
- if(dwUserIndex > dwUserCount)
- {
- m_lLastError = SLS_E_BAD_USER_INDEX;
- return FALSE;
- }
- m_lLastError=SLS_QueryUserInfo(m_strProductID,dwUserIndex,&userInfo);
- return SUCCEEDED(m_lLastError);
- }
-
-